package top.mypath.page;

import lombok.SneakyThrows;
import net.miginfocom.swing.MigLayout;
import org.apache.commons.lang.StringUtils;
import top.mypath.model.ProcessingEnum;
import top.mypath.model.dto.CacheData;
import top.mypath.model.dto.CheckBoxItem;
import top.mypath.model.dto.SheetSelectItem;
import top.mypath.model.vo.ActionImpl;
import top.mypath.service.JCheckBoxChangeListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author LiYalin
 */
public class SecondPage extends JPanel {

    private JTextField filePathField;

    private JTextField fileNameField;

    private Map<String,JCheckBox> checkBoxMap;

    public SecondPage() throws NoSuchFieldException, IllegalAccessException {
        MigLayout migLayout = new MigLayout("wrap 4","[grow,25%][grow,fill,25%][grow,fill,25%][grow,fill,25%]","");
        this.setLayout(migLayout);
        checkBoxMap = new HashMap<>();
        cretePanelLayout();
    }

    private void cretePanelLayout() throws NoSuchFieldException, IllegalAccessException {
        JLabel tableHeadLabel = new JLabel("生成表列：");
        JLabel filePathLabel = new JLabel("文件存储路径：");
        JLabel newFileNameLabel = new JLabel("文件名称：");
        JLabel fileTypeLabel = new JLabel(".xlsx");
        JCheckBox allSelect = new JCheckBox("全选");
        filePathField = new JTextField();
        filePathField.setForeground(Color.GRAY);
        filePathField.setText("双击选择文件存储位置");
        filePathField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (filePathField.getText().equals("双击选择文件存储位置")) {
                    filePathField.setText("");
                    filePathField.setForeground(Color.BLACK);
                }
            }
            @Override
            public void focusLost(FocusEvent e) {
                if (filePathField.getText().equals("")) {
                    filePathField.setText("双击选择文件存储位置");
                    filePathField.setForeground(Color.GRAY);
                }
            }
        });
        filePathField.setToolTipText("双击选择文件存储位置");
        fileNameField = new JTextField();
        this.add(filePathLabel);
        this.add(filePathField,"span 3");
        this.add(newFileNameLabel);
        this.add(fileNameField,"span 2");
        this.add(fileTypeLabel);
        this.add(tableHeadLabel);
        this.add(allSelect,"wrap");
        filePathField.addMouseListener(new MouseListener() {
            @SneakyThrows
            @Override
            public void mouseClicked(MouseEvent e) {
                int clickCount = e.getClickCount();
                if (clickCount == 2) {
                    JFileChooser fileChooser = new JFileChooser();
                    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    int option = fileChooser.showOpenDialog(MainPage.getFrame());
                    if (option == JFileChooser.APPROVE_OPTION) {
                        File file = fileChooser.getSelectedFile();
                        CacheData.setOutputFile(file);
                        CacheData.setOutputFilePath(file.getPath());
                        filePathField.setText(file.getPath());
                    }
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        allSelect.addChangeListener(e -> {
            Collection<JCheckBox> jCheckBoxes = checkBoxMap.values();
            if(allSelect.isSelected()){
                for (JCheckBox jCheckBox : jCheckBoxes) {
                    jCheckBox.setSelected(Boolean.TRUE);
                }
                List<CheckBoxItem> selectedCheckBoxItems = CacheData.getSelectedCheckBoxItems();
                selectedCheckBoxItems=selectedCheckBoxItems.stream().sorted(Comparator.comparingInt(CheckBoxItem::getCellNo)).collect(Collectors.toList());
                CacheData.setSelectedCheckBoxItems(selectedCheckBoxItems);
            }else{
                for (JCheckBox jCheckBox : jCheckBoxes) {
                    jCheckBox.setSelected(Boolean.FALSE);
                }
            }
        });
    }

    public void flushedCheckBox(){
        for(Map.Entry< String,JCheckBox> entry:checkBoxMap.entrySet()){
            this.remove(entry.getValue());
        }
        checkBoxMap.clear();
        List<CheckBoxItem> showCheckBoxItems = CacheData.getShowCheckBoxItems();
        createFixedCheckBox(showCheckBoxItems);
        for (CheckBoxItem showCheckBoxItem : showCheckBoxItems) {
            createFixedCheckBoxItem(showCheckBoxItem);
            showCheckBoxItem.setCellNo(checkBoxMap.size());
        }

    }


    private void createFixedCheckBox(List<CheckBoxItem> showCheckBoxItems){
//        showCheckBoxItems.add(new CheckBoxItem("折扣金额", ProcessingEnum.PADDING,""));
//        showCheckBoxItems.add(new CheckBoxItem("折扣税额", ProcessingEnum.PADDING,""));
//        showCheckBoxItems.add(new CheckBoxItem("折扣率", ProcessingEnum.PADDING,""));
//        showCheckBoxItems.add(new CheckBoxItem("价格方式", ProcessingEnum.PADDING,"0"));
        showCheckBoxItems.add(new CheckBoxItem("税收分类编码版本号", ProcessingEnum.PADDING,"330"));
//        showCheckBoxItems.add(new CheckBoxItem("企业商品编码", ProcessingEnum.PADDING,""));
//        showCheckBoxItems.add(new CheckBoxItem("使用优惠政策标识", ProcessingEnum.PADDING,"0"));
//        showCheckBoxItems.add(new CheckBoxItem("零税率标识", ProcessingEnum.PADDING,""));
//        showCheckBoxItems.add(new CheckBoxItem("优惠政策说明", ProcessingEnum.PADDING,""));
//        showCheckBoxItems.add(new CheckBoxItem("中外合作油气田标识", ProcessingEnum.PADDING,"0"));
    }


    public void createFixedCheckBoxItem(CheckBoxItem showCheckBoxItem){
        String cellName=showCheckBoxItem.getCellName();
        String value=showCheckBoxItem.getValue();
        ActionImpl action = new ActionImpl();
        action.putValue(Action.NAME,cellName);
        if(StringUtils.isNotBlank(value)) {
            action.putValue(Action.SHORT_DESCRIPTION, value);
        }
        JCheckBox jCheckBox = new JCheckBox(action);
        jCheckBox.addChangeListener(new JCheckBoxChangeListener());
        this.add(jCheckBox);
        checkBoxMap.put(cellName,jCheckBox);
        CacheData.getShowCheckBoxItemMap().put(cellName,showCheckBoxItem);
    }

    public void setOutputFile(){
        if(filePathField!=null){
            CacheData.setOutputFilePath(filePathField.getText());
        }
        if(fileNameField!=null){
            CacheData.setOutputFileName(fileNameField.getText());
        }
    }

}
